package com.leoman.dao;

import com.leoman.entity.AppUserGain;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 普通用户收支明细
 * Created by 涂奕恒 on 2016/10/18.
 */
public interface AppUserGainDao extends JpaRepository<AppUserGain, Integer>, JpaSpecificationExecutor<AppUserGain> {

    @Query("select a from AppUserGain a where a.videoRecord.id = ?1 and a.appUser.id = ?2")
    List<AppUserGain> iFindListByVideoRecordIdAndUserId(Integer videoRecordId, Integer userId);

    @Query("select a from AppUserGain a where a.videoRecord.id = ?1")
    List<AppUserGain> iFindListByVideoRecordId(Integer videoRecordId);

    @Query("select a from AppUserGain a where a.appUser.id = ?1 and a.videoRecord is not null")
    List<AppUserGain> iFindListOnlyGainByUserId(Integer userId);

    @Query("select a from AppUserGain a where a.videoRecord is not null order by a.createTime desc")
    List<AppUserGain> iFindListOnlyGain();

    @Query("select a from AppUserGain a where a.videoRecord is not null group by a.appUser.id order by a.money desc")
    List<AppUserGain> iGroupByAppUserForRank();

    @Query("select a from AppUserGain a where a.appUser.id = ?1 and a.requestStatus = 2 group by a.recordDate order by a.recordDate desc")
    Page<AppUserGain> iPageByUserId(Integer userId, Pageable pageable);

    @Query("select a from AppUserGain a where a.videoRecord is null and a.createTime >= ?1")
    List<AppUserGain> findListNotGainByCreateTime(Long createTime);

    @Query("select a from AppUserGain a where a.requestStatus = 2 and a.appUser.id = ?1 and a.recordDate = ?2 order by a.id desc")
    List<AppUserGain> findListByRecordDate(Integer userId, String recordDate);

    @Query("select a from AppUserGain a where a.videoRecord is not null and a.appUser.id = ?1 and a.createTime >= ?2 and a.createTime <= ?3")
    List<AppUserGain> findListByParams(Integer userId, Long startTime, Long endTime);
}